<?php

	/** Classe de modelisation des cotisations au sein du club.
	 * @author Sebastian Mosser
	 * @date 2006
	 * @version 1.0
	 */


class Cotisation implements iPersistantData, iHTMLViewable {

	private $_id;
	private $_label;
	private $_seasonId;
	private $_value;
	
	private static $names = array("Gratuite","Demi - Tarif", "Plein - Tarif", "Administrateur", "Personnel");
	
	public function __construct($l,$s, $v,$id = -1) {
		$this->_label = $l;
		$this->_seasonId = $s;
		$this->_value = $v;
		$this->_id = $id;
	}
	
	/** Implementation de l'interface iHTMLViewable **/
	
	public function displayLong() {
		return $this->_label . " / " . $this->_value . " €";
	}
	
	public function displayShort() {
		return $this->_label;
	}
	
	public function displayInline($scriptUrl="") {
		$season = Season::read($this->_seasonId);
		$name = $season->displayShort();
		$ans = "<tr>$name<td></td>";
		$ans .="<td>$this->_label</td>";
		$ans .= "<td>$this->_value</td></tr>";
	}

	/** Fin de l'implementation de l'interface iHTMLViewable **/
	
	/** Implementation de l'interface iPersistantData**/
	// Creation de la table persistante
	public static function createTable() {
		$query  = " CREATE TABLE IF NOT EXISTS `cotisation` ( \n";
		$query .= "  `id`    INTEGER     NOT NULL PRIMARY KEY auto_increment, \n";
		$query .= "  `label`     VARCHAR(30) NOT NULL, \n";
		$query .= "  `season_id` INTEGER NOT NULL,\n";
		$query .= "   `value`    INTEGER NOT NULL);\n";
		
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	
	}
	// Supression de la table persistante
	public static function dropTable() {
	
		$myDb = Database::getSingleton();
		$myDb->execute("DROP TABLE IF EXISTS `cotisation`");
	
	}

	// Creation de la donnée persistante
	public function create() {
		
		if ($this->_id != -1)
			throw new RuntimeException("Cotisation/create()",
												"this object is still present in database !");
												
		$label = Database::escape($this->_label);
		$season = Database::escape($this->_seasonId);
		$val = Database::escape($this->_value);
		$sql = "INSERT INTO `cotisation` VALUES ('', '$label','$season','$val')";

		$myDb = Database::getSingleton();
		$result = $myDb->executeInsert($sql);
		if ($result[0] != 0)
			$this->_id = $result[0];
		else
			$this->id = -1;
	}
	
	// Lecture d'un element persistant
	public static function read($key) {
		$key = Database::escape($key);
		$sql = "SELECT * FROM `cotisation` WHERE `id` = " .$key;
		
		$myDb = Database::getSingleton();
		$ans = mysql_fetch_assoc($myDb->execute($sql));
		
		if (! $ans)
			throw new RuntimeException("Cotisation/read()","Unknown key '$key'");
		
		return new Cotisation($ans["label"], $ans["season_id"], $ans["value"], $ans["id"]);
		
	}
	// Mise a jour d'un element
	public function update(){
		
		if ($this->_id == -1)
			throw new RuntimeException("ClubFunction/update()",
												"this object wasn't saved in database !");
		
		$label = Database::escape($this->_label);
		$seasonId = Database::escape($this->_seasonId);
		$value = Database::escape($this->_value);
		$sql   = "UPDATE `club_function` SET \n";
		$sql  .= "  `label`     = '$label'    ,\n";
		$sql  .= "  `season_id` = '$seasonId' ,\n";
		$sql  .= "  `value`     = '$value'      \n";
		$sql  .= "  WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	// supression d'un element
	public function delete() {
		if ($this->_id == -1)
			throw new RuntimeException("MoneyType/delete()",
												"this object wasn't saved in database !");
		$sql = "DELETE FROM `cotisation` WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	
	public function tag() {
	 		$tmp = array();
	 		$tmp[0] = "Cotisation";
	 		$tmp[1] = $this->_id;
	 		return $tmp;
	}	
	
	public function soundsLike($o) {
	 		return ($this->_label == $o->_label && $this->_seasonId == $o->_seasonId);
	}
	
	/** Fin de l'implementation de la persistance des données **/
	
	public static function getAsListOnSeason($cotisId, $season) {
		$sql = "SELECT * FROM `cotisation` WHERE `season_id` <= $season ";
		$sql .= " ORDER BY `value` ASC;";
		$myDb = Database::getSingleton();
		$ans = $myDb->execute($sql);
		$resultArray = array();
		
		// Récupération des cotis valides pour la saison
		while (($row = Database::assoc($ans)) != False) {
			$aCotisation = Cotisation::read($row["id"]);
			if (empty($resultArray[$row["label"]]))
				$resultArray[$row["label"]] = $aCotisation;
			else {
				$readSeason = $resultArray[$row["label"]];
				if ($previousSeason->_seasonId < $aCotisation->_seasonId)
					$resultArray[$row["label"]] = $aCotisation;
			}
		}
		
		// Fabrication de la liste :
		$result = "";
		foreach($resultArray as $v) {
			if ($v->_id == $cotisId)
				$result .= "<option value=\"".$v->_id."\" selected>";
			else
				$result .= "<option value=\"".$v->_id."\">";
				
			$result .= $v->_label . " [ " . $v->_value . "€ ]";
			$result .= "</option>";
		}
		return $result;
		
	}
	
	public static function getAsList($cotisId) {
		return self::getAsListOnSeason($cotisId, Login::seasonChoosen(False));
	}



}


?>